/* file : nd_cmmgr.cpp
 * implemention client map manager 
 *
 * 2009-4-25 10:25
 */

#include "nd_appcpp/nd_cmmgr.h"
#include "nd_appcpp/nd_listener.h"
#include "nd_appcpp/nd_session.h"
#include "nd_appcpp/nd_instance.h"

static inline NDSession* GetSessionFromHandle(nd_netui_handle handle)
{
	return static_cast<NDSession*>(nd_session_getdata(handle))  ;
}

NDCmIterator::NDCmIterator(NDListener &listener) 
{
	m_clistener = &listener ;
#ifdef SINGLE_THREAD_MOD	
	m_pSession_mgr = listener.GetSessionMgr() ;
	m_iter = m_pSession_mgr->end() ;
#else 
	cm_iterator.session_id = 0 ;
	pManger = nd_listensrv_get_cmmamager(listener.GetHandle()) ;
#endif
}

NDCmIterator::NDCmIterator(NDInstanceSrv *inst) 
{	
	m_clistener = NULL ;
#ifdef SINGLE_THREAD_MOD
	m_pSession_mgr = NULL;
	if(inst){
		NDListener *plis = inst->GetDeftListener() ;
		if(plis) {
			m_clistener = plis ;
			m_pSession_mgr = plis->GetSessionMgr() ;
			m_iter = m_pSession_mgr->end() ;
		}
	}
#else 
	pManger =  NULL;
	cm_iterator.session_id = 0 ;
	if(inst){
		NDListener *plis = inst->GetDeftListener() ;
		if(plis) {
			pManger = nd_listensrv_get_cmmamager(plis->GetHandle()) ;
			m_clistener = plis ;
		}
	}
#endif

}
NDCmIterator::~NDCmIterator() 
{
#ifdef SINGLE_THREAD_MOD
#else 
	nd_assert(cm_iterator.session_id==0) ;
	if(pManger && cm_iterator.session_id)
		pManger->unlock_iterator(pManger, &cm_iterator) ;
#endif
}

#ifdef SINGLE_THREAD_MOD
NDSession* NDCmIterator::First()
{
	nd_assert(m_pSession_mgr) ;
	m_iter = m_pSession_mgr->begin();
	if(m_iter!= m_pSession_mgr->end()) {
		return &(m_iter->second) ;
	}
	return NULL ;
}

NDSession * NDCmIterator::Next()
{
	nd_assert(m_pSession_mgr) ;
	++m_iter;
	if(m_iter!= m_pSession_mgr->end()) {
		return  &(m_iter->second) ;
	}
	return NULL ;
}

void NDCmIterator::BreakIterator()
{
	nd_assert(m_pSession_mgr) ;
	m_iter = m_pSession_mgr->end() ;
}

NDSession * NDCmIterator::Lock(NDUINT16 sessionID)
{
	nd_assert(m_pSession_mgr) ;
	return m_clistener->FindSession(sessionID) ;
}

NDSession * NDCmIterator::TryLock(NDUINT16 sessionID)
{
	nd_assert(m_pSession_mgr) ;
	return m_clistener->FindSession(sessionID) ;
}

void NDCmIterator::Unlock(NDUINT16 sessionID)
{
	nd_assert(m_pSession_mgr) ;
}
#else 
//////////////////////////////////////////////////////////////////////////

NDSession* NDCmIterator::First()
{
	nd_netui_handle handle =(nd_netui_handle) (pManger->lock_first (pManger,&cm_iterator) );
	if(handle)
		return GetSessionFromHandle(handle) ;
	else 
		return 0 ;
}

NDSession * NDCmIterator::Next()
{
	nd_netui_handle handle =(nd_netui_handle)(pManger->lock_next (pManger,&cm_iterator) );
	if(handle)
		return  GetSessionFromHandle(handle) ;
	else 
		return 0 ;
}

void NDCmIterator::BreakIterator()
{
	pManger->unlock_iterator(pManger, &cm_iterator) ;
}

NDSession * NDCmIterator::Lock(NDUINT16 sessionID)
{
	nd_netui_handle handle = (nd_netui_handle) (pManger->lock(pManger,sessionID) );
	if(handle)
		return GetSessionFromHandle(handle) ;
	else 
		return 0 ;
}

NDSession * NDCmIterator::TryLock(NDUINT16 sessionID)
{
	nd_netui_handle handle = (nd_netui_handle) (pManger->trylock(pManger,sessionID) );
	if(handle)
		return GetSessionFromHandle(handle) ;
	else 
		return 0 ;
}

void NDCmIterator::Unlock(NDUINT16 sessionID)
{
	pManger->unlock(pManger, sessionID) ;
}
NDINT32 NDCmIterator::GetActiveNum()
{
	return (NDINT32) (pManger->connect_num );
}

NDINT32 NDCmIterator::GetCapacity()
{
	return (NDINT32) (pManger->max_conn_num );
}
#endif
